/**
 * Created by Administrator on 2017/4/13.
 */
import {logout} from "./manager/LoginManager";
export var offset = {
	left($el)
	{
		var actualLeft = $el.offsetLeft;
		var current = $el.offsetParent;
		while (current != null)
		{
			actualLeft += current.offsetLeft;
			current = current.offsetParent;
		}

		return actualLeft;
	},
	top($el)
	{
		var actualTop = $el.offsetTop;
		var current = $el.offsetParent;
		while (current != null)
		{
			actualTop += current.offsetTop;
			current = current.offsetParent;
		}

		return actualTop;
	}
};

function traceInTest($name)
{
	trace("测试专用==========================================================启");
	trace($name + "", $name);
	trace("测试专用==========================================================收");

}
window.traceInTest = traceInTest;
export function typeOf(obj)
{
	const toString = Object.prototype.toString;
	const map = {
		'[object Boolean]': 'boolean',
		'[object Number]': 'number',
		'[object String]': 'string',
		'[object Function]': 'function',
		'[object Array]': 'array',
		'[object Date]': 'date',
		'[object RegExp]': 'regExp',
		'[object Undefined]': 'undefined',
		'[object Null]': 'null',
		'[object Object]': 'object'
	};
	return map[toString.call(obj)];
}

export function looseEqual(a, b)
{
	var isObjectA = typeOf(a) == "object";
	var isObjectB = typeOf(b) == "object";
	if (isObjectA && isObjectB)
	{
		try
		{
			return JSON.stringify(a) === JSON.stringify(b)
		}
		catch (e)
		{
			// possible circular reference
			return a === b
		}
	}
	else if (!isObjectA && !isObjectB)
	{
		return String(a) === String(b)
	}
	else
	{
		return false
	}
}

export function oneOf($value, validList)
{
	if (typeOf(validList) == 'array')
	{
		return checkValid(validList);
	}
	else
	{
		var newList = Array.prototype.slice.call(validList);
		if (typeOf(newList) == 'array')
		{
			return checkValid(newList);
		}
	}

	function checkValid($list)
	{
		if ($list.indexOf($value) >= 0)
		{
			return true;
		}
		return false;
	}

}

export function getDays(day1, day2)
{
	// 获取入参字符串形式日期的Date型日期
	var d1 = day1.getDate();
	var d2 = day2.getDate();

	// 定义一天的毫秒数
	var dayMilliSeconds = 1000 * 60 * 60 * 24;

	// 获取输入日期的毫秒数
	var d1Ms = d1.getTime();
	var d2Ms = d2.getTime() - dayMilliSeconds;

	// 定义返回值
	var ret = [];

	// 对日期毫秒数进行循环比较，直到d1Ms 大于等于 d2Ms 时退出循环
	// 每次循环结束，给d1Ms 增加一天
	for (d1Ms; d1Ms <= d2Ms; d1Ms += dayMilliSeconds)
	{

		// 如果ret为空，则无需添加","作为分隔符
		if (ret.length != [])
		{
			// 将给的毫秒数转换为Date日期
			var day = new Date(d1Ms);

			// 获取其年月日形式的字符串
			ret.push(day.getYMD());
		}
		else
		{

			// 否则，给ret的每个字符日期间添加","作为分隔符
			var day = new Date(d1Ms);
			ret.push(day.getYMD());
		}
	}

	return ret; // 或可换为return ret;
}

// 给Date对象添加getYMD方法，获取字符串形式的年月日
Date.prototype.getYMD = function ()
{
	var retDate = this.getFullYear() + "-";  // 获取年份。

	var month = String(this.getMonth() + 1).length == 1 ? '0' + String(this.getMonth() + 1) : String(this.getMonth() + 1);

	retDate += month + "-";    // 获取月份。

	var day = String(this.getDate()).length == 1 ? '0' + String(this.getDate()) : String(this.getDate());

	retDate += day;               // 获取日。
	return retDate;                          // 返回日期。
}

// 给String对象添加getDate方法，使字符串形式的日期返回为Date型的日期
String.prototype.getDate = function ()
{
	var strArr = this.split('-');
	var date = new Date(strArr[0], strArr[1] - 1, strArr[2]);
	return date;
}

export function formatNum($num)
{
	if (isNaN($num))
	{
		return $num;
	}
	if (int($num) > 10000)
	{
		return ($num / 10000).toFixed(2) + "万"
	}
	return $num;
}

export function formatLocation(lng, lat)
{
	//国测局坐标转百度经纬度坐标
	var gcj02tobd09 = coordtransform.gcj02tobd09(lng, lat);
	return gcj02tobd09;
}

export function transferLocation(lng, lat)
{
	//百度经纬度坐标转国测局坐标
	var bd09togcj02 = coordtransform.bd09togcj02(lng, lat);
	return bd09togcj02;
}
export function updateUserInfo(d)
{
	var staticData = g.data.staticData;
	g.data.userInfo.update(d);
	g.data.staticRightPool.removeAll();
	g.data.staticGuidePool.removeAll();
	g.data.staticRightPool.update(staticData["roleRight"]);
	g.data.staticGuidePool.update(g.param.guideList);
	g.data.rightPool.update(d.rights);
}

export function hasGuided($num, $pos)
{
﻿	return $num >> ($pos - 1) & 1;
}

export function dealErr($err)
{
	var msgInfo = g.data.langPool.getDataById("error" + $err.error.code);
	g.ui.toast(msgInfo.content);
	g.ui.hideLoading();
	if ($err.error.code == 1000)
	{
		logout();
	}
}